package com.gxc.dbcore.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.gxc.dbcore.cache.EntityCache;
import com.gxc.dbcore.dao.SqlDao;
import com.gxc.dbcore.service.BeanService;
import com.gxc.dbcore.util.BeanMap;
import com.gxc.dbcore.util.CompareItem;
import com.gxc.dbcore.util.EntityDef;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;
import com.gxc.webcore.util.MapUtil;

@Service("beanService")
public class BeanServiceImpl implements BeanService {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public <T> T get(SqlDao sqlDao, Class<?> clazz, Object idValue) {
		Map params = new HashMap();
		EntityDef entityDef = EntityCache.getEntityDef(clazz);
		params.put("schema", entityDef.getSchema());
		params.put("table", entityDef.getTable());
		params.put("columns", entityDef.getProperties());
		params.put("id", entityDef.getId());
		params.put("idValue", idValue);
		BeanMap beanMap = sqlDao.selectOne("webcore.entity.get", params);
		return (T) MapUtil.map2bean(beanMap, clazz);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int create(SqlDao sqlDao, Object object) {
		Map params = new HashMap();
		EntityDef entityDef = EntityCache.getEntityDef(object.getClass());
		params.put("schema", entityDef.getSchema());
		params.put("table", entityDef.getTable());
		params.put("columnList", entityDef.getKeyValueList(object));
		return sqlDao.insert("webcore.entity.create", params);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int update(SqlDao sqlDao, Object object) {
		EntityDef entityDef = EntityCache.getEntityDef(object.getClass());
		Map params = new HashMap();
		params.put("schema", entityDef.getSchema());
		params.put("table", entityDef.getTable());
		params.put("id", entityDef.getId());
		params.put("idValue", entityDef.getIdValue(object));
		params.put("columnList", entityDef.getKeyValueList(object));
		return sqlDao.update("webcore.entity.update", params);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int delete(SqlDao sqlDao, Class<?> clazz, Object idValue) {
		Map params = new HashMap();
		EntityDef entityDef = EntityCache.getEntityDef(clazz);
		params.put("schema", entityDef.getSchema());
		params.put("table", entityDef.getTable());
		params.put("columns", entityDef.getProperties());
		params.put("id", entityDef.getId());
		params.put("idValue", idValue);
		return sqlDao.delete("webcore.entity.delete", params);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int delete(SqlDao sqlDao, Class<?> clazz, Object[] idValueArray) {
		Map params = new HashMap();
		EntityDef entityDef = EntityCache.getEntityDef(clazz);
		params.put("schema", entityDef.getSchema());
		params.put("table", entityDef.getTable());
		params.put("columns", entityDef.getProperties());
		params.put("id", entityDef.getId());
		params.put("idValueArray", idValueArray);
		return sqlDao.delete("webcore.entity.deleteBatch", params);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public long count(SqlDao sqlDao, Class<?> clazz,
			List<CompareItem> conditionList) {
		EntityDef entityDef = EntityCache.getEntityDef(clazz);
		Map queryParams = new HashMap();
		queryParams.put("schema", entityDef.getSchema());
		queryParams.put("table", entityDef.getTable());
		queryParams.put("conditionList", conditionList);
		if (conditionList != null) {
			int conditionNum = 0;
			for (CompareItem item : conditionList) {
				if (item.getCompareValue() != null) {
					conditionNum++;
				}
			}
			queryParams.put("conditionNum", conditionNum);
		}
		return sqlDao.selectOne("webcore.entity.count", queryParams);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List list(SqlDao sqlDao, Class<?> clazz,
			List<CompareItem> conditionList, String fieldSort) {
		EntityDef entityDef = EntityCache.getEntityDef(clazz);
		Map queryParams = new HashMap();
		queryParams.put("schema", entityDef.getSchema());
		queryParams.put("table", entityDef.getTable());
		queryParams.put("columns", entityDef.getProperties());
		queryParams.put("conditionList", conditionList);
		queryParams.put("fieldSort", fieldSort);
		List resultList = sqlDao.selectList("webcore.entity.list", queryParams);
		return MapUtil.maplist2beanlist(resultList, clazz);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public PageList listPage(SqlDao sqlDao, Class<?> clazz,
			List<CompareItem> conditionList, String fieldSort, PageInfo pageInfo) {
		EntityDef entityDef = EntityCache.getEntityDef(clazz);
		Map queryParams = new HashMap();
		queryParams.put("schema", entityDef.getSchema());
		queryParams.put("table", entityDef.getTable());
		queryParams.put("columns", entityDef.getProperties());
		queryParams.put("conditionList", conditionList);
		queryParams.put("fieldSort", fieldSort);
		if (conditionList != null) {
			int conditionNum = 0;
			for (CompareItem item : conditionList) {
				if (item.getCompareValue() != null) {
					conditionNum++;
				}
			}
			queryParams.put("conditionNum", conditionNum);
		}
		PageList pageList = sqlDao.selectPageList("webcore.entity.list",
				"webcore.entity.count", queryParams, pageInfo);
		pageList.setList(MapUtil.maplist2beanlist(pageList.getList(), clazz));
		return pageList;
	}

}
